<?php
namespace app\admin\logic;
use think\Facade\Db;
use app\common\Apiv;
use app\common\Util;
/**
 * @Author: K.EL
 * @Date:   2020-07-20 00:04:13
 * @Last Modified by:   K.EL
 * @Last Modified time: 2020-07-27 15:11:02
 */
class UserLogic{
	static function getInfo(){
		//cache(true) 开启缓存，后台可以做一个清除缓存的功能，删除所有的缓存
		$token = input('token');
		$info = Db::name('sys_user')
					->field('id,user_name,phone,head_img,status,token')
					->where('token', $token)
					->cache(true)
					->find();
		return Apiv::result(1, 0, '信息获取成功', $info);
	}

	static function getInfoById(){
		$id = input('id',0);
		$info = Db::name('sys_user')
					->field('id,user_name,phone,head_img,status,role_ids,is_admin')
					->where('id', $id)
					->find();
		if($info){
			$info['role_ids'] = explode(',', $info['role_ids']);
			foreach($info['role_ids'] as &$v){
				$v = (int)$v;
			}
		}
		return Apiv::result(1, 0, '信息获取成功', $info);
	}

	static function getLists(){
		$user_name = input('user_name', '');
		$status = input('status', '');
		$pageSize = input('size', 10);
		$obj = Db::name('sys_user')
					->alias('u')
					->field('u.*,CAST(u.status AS CHAR) as status,IFNULL(GROUP_CONCAT(r.role_name),"系统管理员") as role_names')
					->join('sys_role r','FIND_IN_SET(r.id, u.role_ids)','LEFT')
					->group('u.id');
		if($user_name){
			$obj->where('u.user_name|u.phone','like','%'.$user_name.'%');
		}
		if($status){
			$obj->where('u.status', $status);
		}else{
			$obj->where('u.status','exp','>0');
		}
		$lists = $obj->order('u.id desc')->paginate($pageSize);
		return Apiv::result(1, 0, '信息获取成功', $lists);
	}

	static function add(){
		$data = [];
		$data['user_name'] = input('user_name','');
		$data['status'] = input('status', 1);
		$data['add_time'] = date('Y-m-d H:i:s');
		$data['phone'] = input('phone','');
		$data['head_img'] = input('head_img','');
		$password = input('password', '123456');
		$data['salt'] = Util::init()->getChu(6);
		$data['password'] = md5(md5($password).$data['salt']);
		$data['is_admin'] = 0;
		$role_ids = input('role_ids',[]);
		$data['role_ids'] = implode(',', $role_ids);
		$info = Db::name('sys_user')
				->field('id')
				->where('user_name="'.$data['user_name'].'"')
				->whereOr('phone="'.$data['phone'].'"')
				->find();
		if($info){
			return Apiv::result(0, 1103);
		}
		// print_r($data); exit;
		$id = Db::name('sys_user')->insertGetId($data);
		$data['id'] = $id;
		return Apiv::result(1, 0, '新增成功', $data);
	}

	static function edit(){
		$data = [];
		$data['user_name'] = input('user_name','');
		$data['status'] = input('status', 1);
		$data['phone'] = input('phone','');
		$data['head_img'] = input('head_img','');
		$password = input('password', '');
		if($password){
			$data['salt'] = Util::init()->getChu(6);
			$data['password'] = md5(md5($password).$data['salt']);
		}
		$is_admin = input('is_admin',0);
		if($is_admin==0){
			$role_ids = input('role_ids',[]);
			$data['role_ids'] = implode(',', $role_ids);
		}else{
			$data['role_ids'] = '';
		}
		
		$id = input('id',0);
		Db::name('sys_user')->where('id', $id)->update($data);
		$data['id'] = $id;
		return Apiv::result(1, 0, '编辑成功', $data);
	}

	static function update(){
		$data = [];
		$data['user_name'] = input('user_name','');
		$data['status'] = input('status', 1);
		$data['phone'] = input('phone','');
		$data['head_img'] = input('head_img','');
		$password = input('password', '');
		if($password){
			$data['salt'] = Util::init()->getChu(6);
			$data['password'] = md5(md5($password).$data['salt']);
		}
		$id = input('id',0);
		Db::name('sys_user')->where('id', $id)->update($data);
		$data['id'] = $id;
		return Apiv::result(1, 0, '修改成功', $data);
	}
}